﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using You2V.Data;
using You2V.Data.Shared;
using You2V.LocalDBLayer;

namespace LocalDBLayer.Data
{
	public class You2VReportCollection
	{
		public You2VReportCollection()
		{
			Items = new List<You2VReport>();

            StudentDBL StudentDBL = new StudentDBL();
			List<string> CurrentPeriodList = StudentDBL.GetCurrentPeriod();
			foreach (string t in CurrentPeriodList)
				Items.Add(new CurrentPeriodReport(t));
		}

		public List<You2VReport> Items;
	}

	public abstract class You2VReport
	{
		public string name { get; set; }
		public string query { get; set; }
        public string Filter { get; set; }
		public abstract DataTable GetReport();
	}
	public class CurrentPeriodReport : You2VReport
	{
		public CurrentPeriodReport(string PeriodName)
		{ 
			name = PeriodName + "报表";
			_PeriodName = PeriodName;
		}
		private string _PeriodName;
		

        //取得某个学期的报表
        // todo: 加上地区的filter
		//进度，编号，姓名，就读学校，年级班级，建议金额，提醒函件发出日期，老师，紧急情况联系人， 收款，资助人姓名，资助人邮箱，电话，地址，其他备注信息
		public override DataTable GetReport()
		{
			StudentDBL dbl = new StudentDBL();
			DataTable tb = dbl.GetStudentReport();
			DataTable result = BuiltBlankTable();

			foreach (DataRow p_a in tb.Rows)
			{
				DataRow p_r = result.NewRow();
				p_r["学生编号"] = p_a["StudentNumber"];
				p_r["姓名"] = p_a["FullName"];
				p_r["学校"] = p_a["school"];
				p_r["班级"] = p_a["class"];
				p_r["建议资助金额"] = p_a["amount"];
				p_r["老师"] = p_a["teacher"];
				p_r["紧急联系人"] = p_a["contact"];
				p_r["资助人"] = p_a["sponsor"];
				p_r["资助人邮箱"] = p_a["spon_mail"];
				p_r["资助人电话"] = p_a["spon_tel"];
				p_r["资助人地址"] = p_a["spon_Address"];

				try
				{
					DataTable p_action = dbl.GetStudentPeriodAction(_PeriodName, (Guid)p_a["ID"]);
                    if (p_action != null)
                    {
                        foreach (DataRow p_b in p_action.Rows)
                        {
                            string _action = p_b["Action"].ToString();
                            string _amount = p_b["Amount"].ToString();
                            string _actionDate = ((DateTime)p_b["ActionDate"]).ToShortDateString();
                            string _actionTemp = "";
                            if (result.Columns.Contains(_action))
                            {
                                if (StudentActionEnum.items[_action].ShowAccount)
                                {
                                    _actionTemp = string.Format(" {0}元", _amount);
                                }
                                else
                                {
                                    _actionTemp = _actionDate;
                                }
                                //_temp += " :" + _action.MoidifiedBy;

                                if (p_r[_action].ToString() != string.Empty)
                                    p_r[_action] += "\n";
                                p_r[_action] += _actionTemp;
                            }
                        }
                    }
                    else
                    {
                        Debug.WriteLine("GetStudentPeriodAction fail, error 9577, ID=" + (Guid)p_a["ID"]);
                        continue;
                    }
				}
				catch (Exception ex)
				{
					throw ex;
				}
				result.Rows.Add(p_r);
			}
			return result;
		}
		private DataTable BuiltBlankTable()
		{
			DataTable dt = new DataTable("You2VReportCurrentPeriod");
			dt.Columns.Add("学生编号", typeof(string));
			dt.Columns.Add("姓名", typeof(string));
			dt.Columns.Add("学校", typeof(string));
			dt.Columns.Add("班级", typeof(string));
			dt.Columns.Add("建议资助金额", typeof(string));
			

			foreach (StudentActionEnum p_action in StudentActionEnum.items.Values)
			{
				if (p_action.ShowedAsPrimary)
					dt.Columns.Add(p_action.action, typeof(string));
			}
			dt.Columns.Add("备注", typeof(string));

            //为了便于跟踪， 老师/资助人信息放在后面，跟踪情况放在前面
            dt.Columns.Add("老师", typeof(string));
            dt.Columns.Add("紧急联系人", typeof(string));
            dt.Columns.Add("资助人", typeof(string));
            dt.Columns.Add("资助人邮箱", typeof(string));
            dt.Columns.Add("资助人电话", typeof(string));
            dt.Columns.Add("资助人地址", typeof(string));
            return dt;
		}
	}
	public class ArchievedReport : You2VReport
	{
		public override DataTable GetReport()
		{
            //todo， un finished

			if (query != string.Empty)
			{
                DataTable dt = DBAccess.GetDataTable(query);
				return dt;
			}
			return null;
		}

	}

    //一个学生所有时间资助历史的报表
    public class OneStudentReport : You2VReport
    {
        public override DataTable GetReport()
        {
            StudentDBL dbl = new StudentDBL();
            DataTable tb = dbl.GetStudentReport();
            DataTable result = BuiltBlankTable();

            foreach (DataRow p_a in tb.Rows)
            {
                DataRow p_r = result.NewRow();
                p_r["学生编号"] = p_a["StudentNumber"];
                p_r["姓名"] = p_a["FullName"];
                p_r["学校"] = p_a["school"];
                p_r["班级"] = p_a["class"];
                p_r["建议资助金额"] = p_a["amount"];
                p_r["老师"] = p_a["teacher"];
                p_r["紧急联系人"] = p_a["contact"];
                p_r["资助人"] = p_a["sponsor"];
                p_r["资助人邮箱"] = p_a["spon_mail"];
                p_r["资助人电话"] = p_a["spon_tel"];
                p_r["资助人地址"] = p_a["spon_Address"];

                try
                {
                    /*
                    DataTable p_action = dbl.GetStudentPeriodAction(_PeriodName, (Guid)p_a["ID"]);
                    if (p_action != null)
                    {
                        foreach (DataRow p_b in p_action.Rows)
                        {
                            string _action = p_b["Action"].ToString();
                            string _amount = p_b["Amount"].ToString();
                            string _actionDate = ((DateTime)p_b["ActionDate"]).ToShortDateString();
                            string _actionTemp = "";
                            if (result.Columns.Contains(_action))
                            {
                                if (StudentActionEnum.items[_action].ShowAccount)
                                {
                                    _actionTemp = string.Format(" {0}元", _amount);
                                }
                                else
                                {
                                    _actionTemp = _actionDate;
                                }
                                //_temp += " :" + _action.MoidifiedBy;

                                if (p_r[_action].ToString() != string.Empty)
                                    p_r[_action] += "\n";
                                p_r[_action] += _actionTemp;
                            }
                        }
                    }
                    else
                    {
                        Debug.WriteLine("GetStudentPeriodAction fail, error 9577, ID=" + (Guid)p_a["ID"]);
                        continue;
                    }
                     * */
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                result.Rows.Add(p_r);
            }
            return result;
        }

        private DataTable BuiltBlankTable()
        {
            DataTable dt = new DataTable("You2VReportCurrentPeriod");
            dt.Columns.Add("学生编号", typeof(string));
            dt.Columns.Add("姓名", typeof(string));
            dt.Columns.Add("学校", typeof(string));
            dt.Columns.Add("班级", typeof(string));
            dt.Columns.Add("建议资助金额", typeof(string));


            foreach (StudentActionEnum p_action in StudentActionEnum.items.Values)
            {
                if (p_action.ShowedAsPrimary)
                    dt.Columns.Add(p_action.action, typeof(string));
            }
            dt.Columns.Add("备注", typeof(string));

            //为了便于跟踪， 老师/资助人信息放在后面，跟踪情况放在前面
            dt.Columns.Add("老师", typeof(string));
            dt.Columns.Add("紧急联系人", typeof(string));
            dt.Columns.Add("资助人", typeof(string));
            dt.Columns.Add("资助人邮箱", typeof(string));
            dt.Columns.Add("资助人电话", typeof(string));
            dt.Columns.Add("资助人地址", typeof(string));
            return dt;
        }

    }
}
